﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BestLifePOS_Core.Data.Commands;
using BestLifePOS_Core.DomainObjects;
using BestLifePOS_Core.Data.Fascade;
using System.Linq;


namespace BestLifePOS_PresentationLayer
{
    public partial class frmCategories : Form
    {
        public frmCategories()
        {
            InitializeComponent();
        }

        private void frmCategories_Load(object sender, EventArgs e)
        {

        }

        #region CategoryTab
        private void button22_Click(object sender, EventArgs e)
        {
            FillGridExistingCategories();
        }
        private void FillGridExistingCategories()
        {
            //Fill the DataGrid
            List<Category> LvList = CategoryDAO.GetAllCategories();
            DataTable LvTable = new DataTable();
            //dgvGRN.DataSource = LvTable3;

            dgvCategory.DataSource = LvTable;
            LvTable.Columns.Add("CategoryID ");
            LvTable.Columns.Add("CategoryName ");
            LvTable.Columns.Add("Status ");
            LvTable.Columns.Add("Description ");


            foreach (Category LvCategory in LvList)
            {
                LvTable = (DataTable)dgvCategory.DataSource;

                LvTable.Rows.Add(LvTable.NewRow());

                LvTable.Rows[LvTable.Rows.Count - 1][0] = LvCategory.Cat_ID.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][1] = LvCategory.Cat_Name.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][2] = LvCategory.Status.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][3] = LvCategory.Description.ToString();



            }
        }

        private void btnCategoryId_Search_Click(object sender, EventArgs e)
        {
            CategoryFillGridByCATId();
        }

        private void CategoryFillGridByCATId()
        {
            //Fill the DataGrid
            List<Category> LvList = CategoryDAO.GetCategoriesById(txtCatId_Search.Text.ToString());
            DataTable LvTable = new DataTable();
            //dgvGRN.DataSource = LvTable3;

            dgvCategory.DataSource = LvTable;
            LvTable.Columns.Add("CategoryID ");
            LvTable.Columns.Add("CategoryName ");
            LvTable.Columns.Add("Status ");
            LvTable.Columns.Add("Description ");


            foreach (Category LvSubCategory in LvList)
            {
                LvTable = (DataTable)dgvCategory.DataSource;

                LvTable.Rows.Add(LvTable.NewRow());

                LvTable.Rows[LvTable.Rows.Count - 1][0] = LvSubCategory.Cat_ID.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][1] = LvSubCategory.Cat_Name.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][3] = LvSubCategory.Description.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][2] = LvSubCategory.Status.ToString();


            }

        }

        private void btnNewCategory_Click(object sender, EventArgs e)
        {
            txtCategoryid.Text = CategoryDAO.GetNewCategory().ToString();
        }


        private int SaveCategory()
        {


            Category LvCat = CollectCatFigures();
            if (LvCat != null)
            {
                return CategoryDAO.SaveCategory(LvCat);
            }
            else
            {
                return 0;
            }
        }

        private Category CollectCatFigures()
        {

            try
            {

                Category LvCat = new Category();

                LvCat.Cat_ID = txtCategoryid.Text.ToString();
                LvCat.Cat_Name = txtCategoryname.Text.ToString();
                LvCat.Status = cmbCatStatus.Text.ToString();
                LvCat.Description = rtxtCatDescription.Text.ToString();
                LvCat.User_Id = "1";
                LvCat.Date_Create = DateTime.Now;
                LvCat.Last_Modify = DateTime.Now;


                return LvCat;
            }
            catch (Exception)
            {
                return null;
            }
        }

        private void button20_Click(object sender, EventArgs e)
        {
            SaveCategory();
            MessageBox.Show("CATEGORY WAS BSUCCESFULLY SAVED");
        }


        private void btnClearCategory_Click(object sender, EventArgs e)
        {
            txtCategoryid.Text = "";
            txtCategoryname.Text = "";
            rtxtCatDescription.Text = "";
        }

        private void button18_Click(object sender, EventArgs e)
        {
            txtCategoryid.Text = CategoryDAO.GetNewCategory().ToString();
        }

        private void btnCatClear_Click(object sender, EventArgs e)
        {
            txtCategoryid.Text = "";
            txtCategoryname.Text = "";
            rtxtCatDescription.Text = "";
        }

        private void btnCatID_Search_Click(object sender, EventArgs e)
        {
            CategoryFillGridByCATId();
        }


        #endregion

        #region SubCategoryTab


        private void button29_Click(object sender, EventArgs e)
        {
            FillGrid();
        }


        #region SubCategoryDatagridLoad
        private void FillGrid()
        {
            //Fill the DataGrid
            List<Sub_Category> LvList = Sub_CategoryDAO.GetAllSubcategories();
            DataTable LvTable = new DataTable();
            //dgvGRN.DataSource = LvTable3;

            dgvSubCategory.DataSource = LvTable;
            LvTable.Columns.Add("CategoryID ");
            LvTable.Columns.Add("SubCategoryID ");
            LvTable.Columns.Add("CategoryName ");
            LvTable.Columns.Add("SubCategoryName ");
            LvTable.Columns.Add("Status ");
            LvTable.Columns.Add("Description ");


            foreach (Sub_Category LvSubCategory in LvList)
            {
                LvTable = (DataTable)dgvSubCategory.DataSource;

                LvTable.Rows.Add(LvTable.NewRow());

                LvTable.Rows[LvTable.Rows.Count - 1][0] = LvSubCategory.Cat_ID.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][1] = LvSubCategory.Subcat_ID.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][2] = LvSubCategory.Cat_Name.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][3] = LvSubCategory.Sub_Cat_Name.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][4] = LvSubCategory.Status.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][5] = LvSubCategory.Description.ToString();


            }

        }
        #endregion




        private void tabControl5_MouseClick(object sender, MouseEventArgs e)
        {
            FillCategoryDropDown();
        }

        #region SaveSubCategory


        #region CategoryComboLoad
        private void FillCategoryDropDown()
        {
            //Fill the DataGrid
            List<Category> LvList = CategoryDAO.GetAllCategories();
            DataTable LvTable = new DataTable();
            //dgvGRN.DataSource = LvTable3;

            //cmbCategory.DataSource = LvTable;
            LvTable.Columns.Add("CategoryID");
            LvTable.Columns.Add("CategoryName");

            string lvtest = "";

            foreach (Category LvCategory in LvList)
            {
                //LvTable = (DataTable)cmbCategory.DataSource;

                LvTable.Rows.Add(LvTable.NewRow());

                lvtest = LvCategory.Cat_ID.ToString() + '-' + LvCategory.Cat_Name.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][0] = lvtest;
                //LvTable.Rows[LvTable.Rows.Count - 1][0] = LvCategory.Cat_ID.ToString() +'-' + LvCategory.Cat_Name.ToString();
                //LvTable.Rows[LvTable.Rows.Count - 1][1] = LvSubCategory.Subcat_ID.ToString();


            }

            cmbCategory.DisplayMember = "CategoryID";
            cmbCategory.ValueMember = "CategoryID";
            cmbCategory.DataSource = LvTable;
            //cmbCategory.DataBindings();



        }
        #endregion

        private void tabPage19_Click(object sender, EventArgs e)
        {
            FillCategoryDropDown();
        }

        private void button27_Click(object sender, EventArgs e)
        {
            FillGrid2();
        }

        private void button24_Click(object sender, EventArgs e)
        {
            txtSubCatId.Text = Sub_CategoryDAO.GetNewSubCategory().ToString();
        }


        #region SubCategorySearch
        private void FillGrid2()
        {
            //Fill the DataGrid
            List<Sub_Category> LvList = Sub_CategoryDAO.GetSubcategoriesById(txtSubCat_Search.Text.ToString());
            DataTable LvTable = new DataTable();
            //dgvGRN.DataSource = LvTable3;

            dgvSubCategory.DataSource = LvTable;
            LvTable.Columns.Add("CategoryID ");
            LvTable.Columns.Add("SubCategoryID ");
            LvTable.Columns.Add("CategoryName ");
            LvTable.Columns.Add("SubCategoryName ");
            LvTable.Columns.Add("Status ");
            LvTable.Columns.Add("Description ");


            foreach (Sub_Category LvSubCategory in LvList)
            {
                LvTable = (DataTable)dgvSubCategory.DataSource;

                LvTable.Rows.Add(LvTable.NewRow());

                LvTable.Rows[LvTable.Rows.Count - 1][0] = LvSubCategory.Cat_ID.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][1] = LvSubCategory.Subcat_ID.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][2] = LvSubCategory.Cat_Name.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][3] = LvSubCategory.Sub_Cat_Name.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][4] = LvSubCategory.Status.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][5] = LvSubCategory.Description.ToString();


            }

        }
        #endregion




        private int SaveSubCategory()
        {


            Sub_Category LvSubCat = CollectSubCatFigures();
            if (LvSubCat != null)
            {
                return Sub_CategoryDAO.SaveSubCategory(LvSubCat);
            }
            else
            {
                return 0;
            }
        }

        private Sub_Category CollectSubCatFigures()
        {

            try
            {

                Sub_Category LvSubCat = new Sub_Category();

                LvSubCat.Cat_ID = cmbCategory.Text.ToString().Substring(0, 7);
                LvSubCat.Subcat_ID = txtSubCatId.Text.ToString();
                LvSubCat.Sub_Cat_Name = txtSubCatName.Text.ToString();
                LvSubCat.Status = cmbAvailable.Text.ToString();
                LvSubCat.Description = rtxtDes.Text.ToString();
                LvSubCat.User_Id = "1";
                LvSubCat.Date_Create = DateTime.Now;
                LvSubCat.Last_Modify = DateTime.Now;


                return LvSubCat;
            }
            catch (Exception)
            {
                return null;
            }
        }

        private void button26_Click(object sender, EventArgs e)
        {
            SaveSubCategory();
            MessageBox.Show("SUBCATEGORY WAS BSUCCESFULLY SAVED");
        }

        private void btnClearSubCat_Click(object sender, EventArgs e)
        {
            txtSubCatId.Text = "";
        }




        #endregion

        private void button18_Click_1(object sender, EventArgs e)
        {
            txtCategoryid.Text = CategoryDAO.GetNewCategory().ToString();
        }


        #endregion

        private void btnCatSave_Click(object sender, EventArgs e)
        {

        }
    }
}
